package com.znf.config;

import com.znf.base.Result;
import com.znf.base.enums.ResultCodeEnum;
import com.znf.base.exception.BizException;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常处理器
     */
    @ExceptionHandler(value = BizException.class)
    public Result<Object> bizExceptionHandler(Exception e) {
        log.error("业务异常->", e);
        BizException exception = (BizException) e;
        return new Result<>(exception.getCode(), exception.getMsg());
    }

    /**
     * 认证异常处理器
     */

    @ExceptionHandler(value = JwtException.class)
    public Result<Object> jwtExceptionHandler(Exception e) {
        log.error("业务异常->", e);
        JwtException exception = (JwtException) e;
        return new Result<>(ResultCodeEnum.FAIL.getCode(), exception.getMessage());
    }

    /**
     * 其他异常处理器（兜底）
     */
    @ExceptionHandler(value = Exception.class)
    public Result<Object> exceptionHandler(Exception e) {
        log.error("系统发生异常->", e);
        return new Result<>(ResultCodeEnum.ERROR);
    }
}
